﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using Zsea.HTML;

namespace Zsea.Browser
{
public class Form
{
    // Fields
    private List<NameValue> _nv;
    //[CompilerGenerated]
    //private string <Action>k__BackingField;
    //[CompilerGenerated]
    //private Method <Method>k__BackingField;

    // Methods
    public Form()
    {
        this._nv = new List<NameValue>();
    }

    public Form(string forms)
    {
        this._nv = new List<NameValue>();
        Document document = new Document();
        document.Load(forms);
        string str = string.Empty;
        string str2 = "get";
        try
        {
            str = document.DocumentNode.Attributes["action"].Value;
        }
        catch
        {
        }
        try
        {
            str2 = document.DocumentNode.Attributes["method"].Value;
            str2 = str2.Trim();
        }
        catch
        {
        }
        this.Action = str;
        if (str2.Trim().ToUpper() == "POST")
        {
            this.Method = Method.Post;
        }
        else
        {
            this.Method = Method.Get;
        }
        foreach (Node node in document.DocumentNode.FindAll("input"))
        {
            string str3 = string.Empty;
            string innerHTML = string.Empty;
            string str5 = string.Empty;
            try
            {
                str3 = node.Attributes["type"].Value;
            }
            catch
            {
            }
            try
            {
                innerHTML = node.Attributes["value"].Value;
            }
            catch
            {
            }
            try
            {
                str5 = node.Attributes["name"].Value;
            }
            catch
            {
            }
            if (string.IsNullOrEmpty(str5))
            {
                str5 = "noname";
            }
            switch (str3.ToUpper())
            {
                case "HIDDEN":
                case "TEXT":
                case "BUTTON":
                case "PASSWORD":
                case "IMAGE":
                case "FILE":
                case "RESET":
                {
                    this.Attach(str5, innerHTML);
                    continue;
                }
                case "CHECKBOX":
                case "RADIO":
                {
                    try
                    {
                        Zsea.HTML. Attribute attribute1 = node.Attributes["checked"];
                        this.Attach(str5, innerHTML);
                    }
                    catch
                    {
                    }
                    continue;
                }
                case "SELECT":
                    break;

                case "TEXTAREA":
                {
                    this.Attach(str5, node.InnerHTML);
                    continue;
                }
                default:
                {
                    continue;
                }
            }
            List<Node> list2 = node.Children.FindAll(o=>{
                if (!(o.Name.ToUpper() == "OPTION"))
                    {
                        return false;
                    }
                    return o.Attributes.Exists(p => p.Name.ToUpper() == "SELECTED");
            });
            Node node2 = node.SingleNode("option");
            if (node2 == null)
            {
                this.Attach(str5, string.Empty);
            }
            else
            {
                if (list2.Count > 0)
                {
                    node2 = list2[0];
                }
                try
                {
                    innerHTML = node2.Attributes["value"].Value;
                }
                catch
                {
                    innerHTML = node2.InnerHTML;
                }
                this.Attach(str5, innerHTML);
                continue;
            }
        }
    }

    private void _remove(List<NameValue> lnv)
    {
        foreach (NameValue value2 in lnv)
        {
            this._nv.Remove(value2);
        }
    }

    public Form Attach(string name, string value)
    {
        NameValue item = new NameValue();
        item.Name = name;
        item.Value = value;
        this._nv.Add(item);
        return this;
    }

    public bool Exists(string name)
    {
        return this._nv.Exists(delegate (NameValue o) {
            return o.Name.ToUpper() == name.ToUpper();
        });
    }

    public bool Exists(string name, string value)
    {
        return this.Exists(name, value, true);
    }

    public bool Exists(string name, string value, bool IgnoreCase)
    {
        return this._nv.Exists(delegate (NameValue o) {
            if (IgnoreCase)
            {
                return (o.Name.ToUpper() == name.ToUpper()) && (value.ToUpper() == o.Value.ToUpper());
            }
            return (o.Name.ToUpper() == name.ToUpper()) && (value == o.Value);
        });
    }

    private List<NameValue> getNameValue(string name, string value, bool IgnoreCase)
    {
        List<NameValue> list = new List<NameValue>();
        foreach (NameValue value2 in this._nv)
        {
            if (value == null)
            {
                if (value2.Name.ToUpper() == name.ToUpper())
                {
                    list.Add(value2);
                }
            }
            else
            {
                if (IgnoreCase)
                {
                    if ((value2.Name.ToUpper() == name.ToUpper()) && (value2.Value.ToUpper() == value.ToUpper()))
                    {
                        list.Add(value2);
                    }
                    continue;
                }
                if ((value2.Name.ToUpper() == name.ToUpper()) && (value2.Value == value))
                {
                    list.Add(value2);
                }
            }
        }
        return list;
    }

    public Form Remove(string name)
    {
        this._remove(this.getNameValue(name, null, true));
        return this;
    }

    public Form Remove(string name, int index)
    {
        try
        {
            List<NameValue> lnv = new List<NameValue>();
            lnv.Add(this.getNameValue(name, null, true)[index]);
            this._remove(lnv);
        }
        catch
        {
        }
        return this;
    }

    public Form Remove(string name, string value)
    {
        this._remove(this.getNameValue(name, value, true));
        return this;
    }

    public Form Remove(string name, string value, bool IgonreCase)
    {
        this._remove(this.getNameValue(name, value, IgonreCase));
        return this;
    }

    public Form Remove(string name, string value, int index)
    {
        try
        {
            List<NameValue> lnv = new List<NameValue>();
            lnv.Add(this.getNameValue(name, value, true)[index]);
            this._remove(lnv);
        }
        catch
        {
        }
        return this;
    }

    public Form Remove(string name, string value, bool IgonreCase, int index)
    {
        try
        {
            List<NameValue> lnv = new List<NameValue>();
            lnv.Add(this.getNameValue(name, value, IgonreCase)[index]);
            this._remove(lnv);
        }
        catch
        {
        }
        return this;
    }

    public Form SetValue(string name, string value)
    {
        List<NameValue> list = this.getNameValue(name, null, true);
        if (list.Count > 0)
        {
            foreach (NameValue value2 in list)
            {
                value2.Value = value;
            }
        }
        else
        {
            this.Attach(name, value);
        }
        return this;
    }

    public Form SetValue(string name, int index, string value)
    {
        List<NameValue> list = this.getNameValue(name, null, true);
        if (list.Count > index)
        {
            list[index].Value = value;
        }
        else
        {
            this.Attach(name, value);
        }
        return this;
    }

    public override string ToString()
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < this._nv.Count; i++)
        {
            if (i > 0)
            {
                builder.Append("&");
            }
            builder.Append(string.Format("{0}={1}", this._nv[i].Name, this._nv[i].Value));
        }
        return builder.ToString();
    }

    public string ToString(Encoding encoding)
    {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < this._nv.Count; i++)
        {
            if (i > 0)
            {
                builder.Append("&");
            }
            builder.Append(string.Format("{0}={1}", this._nv[i].Name, HttpUtility.UrlEncode(this._nv[i].Value, encoding)));
        }
        return builder.ToString();
    }

    // Properties
    public string Action
    {
        get;private set;
    }

    public int Count
    {
        get
        {
            return this._nv.Count;
        }
    }

    public string this[string name]
    {
        get
        {
            List<NameValue> list = this.getNameValue(name, null, true);
            if (list.Count == 1)
            {
                return list[0].Value;
            }
            if (list.Count <= 1)
            {
                return null;
            }
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < list.Count; i++)
            {
                if (i != 0)
                {
                    builder.Append(",");
                }
                builder.Append(list[i].Value);
            }
            return builder.ToString();
        }
        set
        {
            List<NameValue> list = this.getNameValue(name, null, true);
            if (list.Count > 0)
            {
                foreach (NameValue value2 in list)
                {
                    value2.Value = value;
                }
            }
            else
            {
                this.Attach(name, value);
            }
        }
    }

    public string this[int index]
    {
        get
        {
            return this._nv[index].Value;
        }
        set
        {
            this._nv[index].Value = value;
        }
    }

    public string this[string name, int index]
    {
        get
        {
            return this.getNameValue(name, null, true)[index].Value;
        }
        set
        {
            this.getNameValue(name, null, true)[index].Value = value;
        }
    }

    public Method Method
    {
        
        get;
        private set;
    }

    // Nested Types
    private class NameValue
    {

        // Properties
        public string Name
        {
            get;
            set;
        }

        public string Value
        {
            get;
            set;
        }
    }
}

 

 


 

 

}
